package com.ltsznh.android.locationreporting.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.ltsznh.android.locationreporting.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 13-8-27.
 */
public class SQLiteHelper extends SQLiteOpenHelper {
    protected Context ctx;
    protected SQLiteDatabase initDb;

    public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.ctx = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        initDb = db;
        Log.v("db", "onCreateDb  ----  start");
        //device_type
        String sql = "CREATE TABLE lr_details ( " +
                "    id             VARCHAR( 30 )  PRIMARY KEY" +
                "                                  NOT NULL," +
                "    userCode       VARCHAR( 20 )," +
                "    userName       VARCHAR( 20 )," +
                "    phoneId        VARCHAR( 20 )," +
                "    phoneName      VARCHAR( 20 )," +
                "    provider       VARCHAR( 10 )," +
                "    reportDateTime INTEGER," +
                "    reportDate     DATE," +
                "    reportTime     DATETIME," +
                "    latitude       NUMERIC," +
                "    longitude      NUMERIC," +
                "    accuracy       NUMERIC," +
                "    altitude       NUMERIC," +
                "    bearing        NUMERIC," +
                "    speed          NUMERIC," +
                "    entryDate      DATE," +
                "    entryTime      DATETIME," +
                "    addressName    VARCHAR( 50 )," +
                "    isSync         CHAR( 1 )      DEFAULT ( 'N' ) " +
                ");";
        db.execSQL(sql);


        Log.v("db", "onCreateDb  ----  end");

        try {//写入初始化数据
            this.initData();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * 写入初始化数据
     *
     * @throws IOException
     */
    public void initData() throws IOException {
        Log.v("db", "initData - start");
        InputStream myInput = ctx.getResources().openRawResource(R.raw.initdata);
        InputStreamReader reader = new InputStreamReader(myInput);
        BufferedReader bReader = new BufferedReader(reader);

        // Path to the just created empty db
//        String outFileName = "/data/data/com.ltsznh.android.familybooks/databases/" + dbo.fbSQLiteName,
        String str;

        // Open the empty db as the output stream
//        FileWriter myOutput = null;

//        myOutput = new FileWriter(outFileName, true);
        //exec your SQL line by line.
        while ((str = bReader.readLine()) != null) initDb.execSQL(str);
        Log.v("db", "initData - end");
        // Close the streams
//        myOutput.flush();
//        myOutput.close();
//        myInput.close();
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
//        Log.v("db", "onUpgrade - start  old:" + oldVersion + " new:" + newVersion);
//        initDb = sqLiteDatabase;
//        if(oldVersion == 1 && newVersion == 2){
//            sqLiteDatabase.execSQL("ALTER TABLE lr_details " +
//                    " ADD COLUMN [isSync] CHAR( 1 )  DEFAULT ( 'N' );");
//        }
//        Log.v("TTTT","onUpgrade" + oldVersion + " " + newVersion);
//
//        Log.v("db", "onUpgrade - end");

    }
}
